﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AddIn.EA.Architecture.Presentation.Controls
{
    public partial class UcLayers : UserControl
    {
        #region Properties
        [Browsable(false)]
        public bool CkUICommunicationChecked { get { return ckUICommunication.Checked; } set { ckUICommunication.Checked = value; } }
        [Browsable(false)]
        public bool CkUIModelChecked { get { return ckUIModel.Checked; } set { ckUIModel.Checked = value; } }
        [Browsable(false)]
        public bool CkUIPresenterChecked { get { return ckUIPresenter.Checked; } set { ckUIPresenter.Checked = value; } }
        [Browsable(false)]
        public bool CkServiceBusinessEntittyChecked { get { return ckServiceBusinessEntitty.Checked; } set { ckServiceBusinessEntitty.Checked = value; } }
        [Browsable(false)]
        public bool CkServiceBusinessLogicChecked { get { return ckServiceBusinessLogic.Checked; } set { ckServiceBusinessLogic.Checked = value; } }
        [Browsable(false)]
        public bool CkServiceDataContractChecked { get { return ckServiceDataContract.Checked; } set { ckServiceDataContract.Checked = value; } }
        [Browsable(false)]
        public bool CkServiceResourceAccessChecked { get { return ckServiceResourceAccess.Checked; } set { ckServiceResourceAccess.Checked = value; } }
        [Browsable(false)]
        public bool CkServiceServiceContractImplementationChecked { get { return ckServiceServiceContractImplementation.Checked; } set { ckServiceServiceContractImplementation.Checked = value; } }
        [Browsable(false)]
        public bool CkProcessCommunicationChecked { get { return ckProcessCommunication.Checked; } set { ckProcessCommunication.Checked = value; } }
        [Browsable(false)]
        public bool CkProcessProcessClientChecked { get { return ckProcessProcessClient.Checked; } set { ckProcessProcessClient.Checked = value; } }
        [Browsable(false)]
        public bool CkProcessProcessEntityChecked { get { return ckProcessProcessEntity.Checked; } set { ckProcessProcessEntity.Checked = value; } }
        [Browsable(false)]
        public bool CkProcessProcessImplementationChecked { get { return ckProcessProcessImplementation.Checked; } set { ckProcessProcessImplementation.Checked = value; } }
        [Browsable(false)]
        public bool CkProcessProcessLogicChecked { get { return ckProcessProcessLogic.Checked; } set { ckProcessProcessLogic.Checked = value; } }
        [Browsable(false)]
        public bool CkProcessResourceAccessChecked { get { return ckProcessResourceAccess.Checked; } set { ckProcessResourceAccess.Checked = value; } }
        [Browsable(false)]
        public bool IsAnyLayerChecked { get { return GetLayerCheckedState(); } }
        #endregion

        #region Fire Events
        public event Action LayerCheckedChange;
        #endregion

        #region Initializing Control
        public UcLayers()
        {
            InitializeComponent();
        }
        #endregion

        #region Events
        private void ckUIModel_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckUIPresenter_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckUICommunication_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckServiceServiceContractImplementation_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckServiceDataContract_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckServiceBusinessEntitty_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckServiceBusinessLogic_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckServiceResourceAccess_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckProcessProcessImplementation_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckProcessProcessLogic_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckProcessProcessEntity_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckProcessResourceAccess_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckProcessCommunication_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckProcessProcessClient_CheckedChanged(object sender, EventArgs e)
        {
            ActionLayerCheckedChangeEvent();
        }
        private void ckUIAll_CheckedChanged(object sender, EventArgs e)
        {
            SetAllChecksUI(ckUIAll.Checked);
        }
        private void ckServiceAll_CheckedChanged(object sender, EventArgs e)
        {
            SetAllChecksService(ckServiceAll.Checked);
        }
        private void ckProcessAll_CheckedChanged(object sender, EventArgs e)
        {
            SetAllChecksProcess(ckProcessAll.Checked);
        }
        #endregion

        #region Methods
        private bool GetLayerCheckedState()
        {
            return ckUICommunication.Checked ||
                   ckUIModel.Checked ||
                   ckUIPresenter.Checked ||
                   ckServiceBusinessEntitty.Checked ||
                   ckServiceBusinessLogic.Checked ||
                   ckServiceDataContract.Checked ||
                   ckServiceResourceAccess.Checked ||
                   ckServiceServiceContractImplementation.Checked ||
                   ckProcessCommunication.Checked ||
                   ckProcessProcessClient.Checked ||
                   ckProcessProcessEntity.Checked ||
                   ckProcessProcessImplementation.Checked ||
                   ckProcessProcessLogic.Checked ||
                   ckProcessResourceAccess.Checked;
        }
        public void SetAllChecks(bool status)
        {
            SetAllChecksUI(status);
            SetAllChecksService(status);
            SetAllChecksProcess(status);
        }
        public void SetAllChecksUI(bool status)
        {
            ckUICommunication.Checked = ckUICommunication.Enabled ? status : ckUICommunication.Checked;
            ckUIModel.Checked = ckUIModel.Enabled ? status : ckUIModel.Checked;
            ckUIPresenter.Checked = ckUIPresenter.Enabled ? status : ckUIPresenter.Checked;
        }
        public void SetAllChecksService(bool status)
        {
            ckServiceBusinessEntitty.Checked = ckServiceBusinessEntitty.Enabled ? status : ckServiceBusinessEntitty.Checked;
            ckServiceBusinessLogic.Checked = ckServiceBusinessLogic.Enabled ? status : ckServiceBusinessLogic.Checked;
            ckServiceDataContract.Checked = ckServiceDataContract.Enabled ? status : ckServiceDataContract.Checked;
            ckServiceResourceAccess.Checked = ckServiceResourceAccess.Enabled ? status : ckServiceResourceAccess.Checked;
            ckServiceServiceContractImplementation.Checked = ckServiceServiceContractImplementation.Enabled ? status : ckServiceServiceContractImplementation.Checked;
        }
        public void SetAllChecksProcess(bool status)
        {
            ckProcessCommunication.Checked = ckProcessCommunication.Enabled ? status : ckProcessCommunication.Checked;
            ckProcessProcessClient.Checked = ckProcessProcessClient.Enabled ? status : ckProcessProcessClient.Checked;
            ckProcessProcessEntity.Checked = ckProcessProcessEntity.Enabled ? status : ckProcessProcessEntity.Checked;
            ckProcessProcessImplementation.Checked = ckProcessProcessImplementation.Enabled ? status : ckProcessProcessImplementation.Checked;
            ckProcessProcessLogic.Checked = ckProcessProcessLogic.Enabled ? status : ckProcessProcessLogic.Checked;
            ckProcessResourceAccess.Checked = ckProcessResourceAccess.Enabled ? status : ckProcessResourceAccess.Checked;
        }
        public List<ComponentLogic.StandardProject.Builders.ElementBuilder> GetLayerBuilders()
        {
            List<ComponentLogic.StandardProject.Builders.ElementBuilder> result = new List<AddIn.EA.Architecture.ComponentLogic.StandardProject.Builders.ElementBuilder>();

            //UI Builders
            if (CkUICommunicationChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.UI.ElementBuilderCommunication());
            if (CkUIModelChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.UI.ElementBuilderModel());
            if (CkUIPresenterChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.UI.ElementBuilderPresenter());
            //Service Builders
            if (CkServiceBusinessEntittyChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Service.ElementBuilderBusinessEntity());
            if (CkServiceBusinessLogicChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Service.ElementBuilderBusinessLogic());
            if (CkServiceDataContractChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Service.ElementBuilderDataContract());
            if (CkServiceResourceAccessChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Service.ElementBuilderResourceAccess());
            if (CkServiceServiceContractImplementationChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Service.ElementBuilderServiceContractImplementation());
            //Process Builders
            if (CkProcessCommunicationChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessCommunication());
            if (CkProcessProcessClientChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessClient());
            if (CkProcessProcessEntityChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessEntity());
            if (CkProcessProcessImplementationChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessImplementation());
            if (CkProcessProcessLogicChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessLogic());
            if (CkProcessResourceAccessChecked)
                result.Add(new ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessResourceAccess());

            return result;
        }
        public void SetLayersBuilders(List<ComponentLogic.StandardProject.Builders.ElementBuilder> builders)
        {
            SetAllChecks(false);
            if (builders != null)
            {
                foreach (ComponentLogic.StandardProject.Builders.ElementBuilder itemBuilder in builders)
                {
                    //UI Builders
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.UI.ElementBuilderCommunication)
                        CkUICommunicationChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.UI.ElementBuilderModel)
                        CkUIModelChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.UI.ElementBuilderPresenter)
                        CkUIPresenterChecked = true;
                    //Service Builders
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Service.ElementBuilderBusinessEntity)
                        CkServiceBusinessEntittyChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Service.ElementBuilderBusinessLogic)
                        CkServiceBusinessLogicChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Service.ElementBuilderDataContract)
                        CkServiceDataContractChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Service.ElementBuilderResourceAccess)
                        CkServiceResourceAccessChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Service.ElementBuilderServiceContractImplementation)
                        CkServiceServiceContractImplementationChecked = true;
                    //Process Builders
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessCommunication)
                        CkProcessCommunicationChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessClient)
                        CkProcessProcessClientChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessEntity)
                        CkProcessProcessEntityChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessImplementation)
                        CkProcessProcessImplementationChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessLogic)
                        CkProcessProcessLogicChecked = true;
                    if (itemBuilder is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessResourceAccess)
                        CkProcessResourceAccessChecked = true;
                }
            }
        }
        public void SetLayersBuilders(List<ComponentLogic.StandardProject.Builders.ElementBuilder> builders, bool disableNonSelectedItems)
        {
            SetLayersBuilders(builders);
            if (disableNonSelectedItems)
            {
                //UI Builders
                ckUICommunication.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.UI.ElementBuilderCommunication);
                ckUIModel.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.UI.ElementBuilderModel);
                ckUIPresenter.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.UI.ElementBuilderPresenter);
                ckUIAll.Enabled = ckUICommunication.Enabled ||
                                  ckUIModel.Enabled ||
                                  ckUIPresenter.Enabled;
                //Service Builders
                ckServiceBusinessEntitty.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Service.ElementBuilderBusinessEntity);
                ckServiceBusinessLogic.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Service.ElementBuilderBusinessLogic);
                ckServiceDataContract.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Service.ElementBuilderDataContract);
                ckServiceResourceAccess.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Service.ElementBuilderResourceAccess);
                ckServiceServiceContractImplementation.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Service.ElementBuilderServiceContractImplementation);
                ckServiceAll.Enabled = ckServiceBusinessEntitty.Enabled ||
                                       ckServiceBusinessLogic.Enabled ||
                                       ckServiceDataContract.Enabled ||
                                       ckServiceResourceAccess.Enabled ||
                                       ckServiceServiceContractImplementation.Enabled;
                //Process Builders
                ckProcessCommunication.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessCommunication);
                ckProcessProcessClient.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessClient);
                ckProcessProcessEntity.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessEntity);
                ckProcessProcessImplementation.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessImplementation);
                ckProcessProcessLogic.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessLogic);
                ckProcessResourceAccess.Enabled = builders.Exists(it => it is ComponentLogic.StandardProject.Builders.Process.ElementBuilderProcessResourceAccess);
                ckProcessAll.Enabled = ckProcessCommunication.Enabled ||
                                       ckProcessProcessClient.Enabled ||
                                       ckProcessProcessEntity.Enabled ||
                                       ckProcessProcessImplementation.Enabled ||
                                       ckProcessProcessLogic.Enabled ||
                                       ckProcessResourceAccess.Enabled;
            }
        }
        private void ActionLayerCheckedChangeEvent()
        {
            if (LayerCheckedChange != null)
                LayerCheckedChange();
        }
        #endregion
    }
}
